//
//  SchoolViewModel.swift
//  HoneyBee
//
//  Created by point on 2018/2/25.
//  Copyright © 2018年 point. All rights reserved.
//

import UIKit
import RxCocoa
import RxSwift

class SchoolViewModel {
    weak var vc: HomeViewController?
    var modelObserable : [SchoolModel] =  [SchoolModel]()
    var publishSubData = PublishSubject<mpLoadData>()
    fileprivate var page: Int = 1 //当前的页数
    fileprivate lazy var bag: DisposeBag =  DisposeBag()
    var keyWord = ""
    
    
    init() {
        publishSubData.subscribe(onNext: { [weak self] (type:mpLoadData) in
            self?.loadData(type: type)
        }).disposed(by: bag)
    }
}


extension SchoolViewModel {
    
    /*
     let dict:Dictionary = [
     "longitude":"1",
     "latitude":"2",
     "pageNo":"1",
     "pageSize":"10",
     "timestamp":"2",
     "sign":"2"
     ] as [String : Any]
     
     let str = dict.formatApi()
     
     //?longitude=1&latitude=2&pageNo=1&pageSize=10&timestamp=2&sign=2
     
     NetworkTools.shareInstance.loadData(.post, URLString: kqueryInstitutionList+str, parameters: dict) { (result, error) in
     
     guard let resultDict = result as? [String : NSObject] else { return }
     guard let dataArray = resultDict["data"] as? [[String : NSObject]] else { return }
     
     
     self.modelArr = SchoolModel.tmSpeelBreakModelArr(dataArray) as! [SchoolModel]
     self.tableView.reloadData()
     
     
     }
     */
    func loadData(type:mpLoadData) {
        if type == .mpLoadDataUp {
            self.page = 1
            let  longitudeStr =  NSString(format: "%f" , (vc?.currentLocation.coordinate.longitude)!)
            let latitudeStr =    NSString(format: "%f" , (vc?.currentLocation.coordinate.latitude)!)  
            
         
            let dict:Dictionary = [
                "longitude":longitudeStr,
                "latitude":latitudeStr,
                "search":keyWord,
                "pageNo":"1",
                "pageSize":"10",
                "timestamp":"2",
                "sign":"2"
                ] as [String : Any]
            let str = dict.formatApi()
            
            
            
            NetworkTools.shareInstance.loadData(.post, URLString: kqueryInstitutionList+str, parameters: dict, finishedCallback: { [unowned self] (result, error) in
                self.vc?.tableView.mj_header.endRefreshing()
                if !error {
                    guard let resultDict = result as? [String : NSObject] else { return }
                    guard let dataArray = resultDict["data"] as? [[String : NSObject]] else { return }
                    let modelArr = SchoolModel.tmSpeelBreakModelArr(dataArray) as! [SchoolModel]
                    self.modelObserable.removeAll()
                    self.modelObserable =  self.modelObserable + modelArr
                    self.vc?.tableView.reloadData()
                    self.page = self.page + 1
                } else {
                    self.vc?.showNetworkErrorView(action: #selector(self.vc?.networkError))
                }
            })
        }else {
            let  longitudeStr =  NSString(format: "%f" , (vc?.currentLocation.coordinate.longitude)!)
            let latitudeStr =    NSString(format: "%f" , (vc?.currentLocation.coordinate.latitude)!)
            let dict:Dictionary = [
                "longitude":longitudeStr,
                "latitude":latitudeStr,
                "pageNo":String(self.page),
                "pageSize":"10",
                "search":keyWord,
                "timestamp":"2",
                "sign":"2"
                ] as [String : Any]
            let str = dict.formatApi()
            
            
            
            NetworkTools.shareInstance.loadData(.post, URLString: kqueryInstitutionList+str, parameters: dict, finishedCallback: { [unowned self] (result, error) in
            
                if !error {
                    guard let resultDict = result as? [String : NSObject] else { return }
                    guard let dataArray = resultDict["data"] as? [[String : NSObject]] else { return }
                    let modelArr = SchoolModel.tmSpeelBreakModelArr(dataArray) as! [SchoolModel]
                    if modelArr.count > 0 {
                        self.vc?.tableView.mj_footer.endRefreshing()
                        self.modelObserable =  self.modelObserable + modelArr
                        self.vc?.tableView.reloadData()
                        self.page = self.page + 1
                    }else {
                         self.vc?.tableView.mj_footer.endRefreshingWithNoMoreData()
                    }
                   
                } else {
                    self.vc?.tableView.mj_footer.endRefreshing()
                    //self.vc?.hudLoadJuHua()
                }
            })
        }
    }
}
